package com.bocloud.cmp.service.resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bocloud.cmp.dao.AlarmDao;
import com.bocloud.cmp.entity.resource.Alarm;
import com.bocloud.cmp.interfaces.resource.AlarmService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.google.common.collect.Maps;

/**
 * 
 * @author songsong
 *
 */
@Service("alarmService")
public class AlarmServiceImpl implements AlarmService {

	private static Logger logger = LoggerFactory.getLogger(AlarmServiceImpl.class);
	@Autowired
	private AlarmDao alarmDao;

	@Override
	public BsmResult list(int page, int rows, List<Param> params, Map<String, String> sorter, Boolean simple)
			throws Exception {
		List<Alarm> list = null;
		List<SimpleBean> beans = null;
		int total = 0;
		GridBean gridBean = null;
		try {
			if (null == params) {
				params = new ArrayList<>();
			}
			if (null == sorter) {
				sorter = Maps.newHashMap();
			}
			sorter.put("gmtTrigger", Common.ONE);//edit 触发时间排序
			total = this.alarmDao.count(params);
			if (simple) {
				beans = this.alarmDao.list(params, sorter);
				gridBean = new GridBean(1, 1, total, beans);
			} else {
				list = this.alarmDao.list(page, rows, params, sorter);
			}
			gridBean = GridHelper.getBean(page, rows, total, list);
			return new BsmResult(true, gridBean, "查询成功");
		} catch (Exception e) {
			logger.error("List alarm fail:", e);
			return new BsmResult(false, "查询失败", null, null);
		}
	}

	@Override
	public BsmResult detail(Long id) {
		try {
			Alarm alarm = alarmDao.query(id);
			if (null == alarm) {
				return new BsmResult(false, "数据不存在");
			}
			return new BsmResult(true, alarm, "查询详情成功");
		} catch (Exception e) {
			logger.error("query alarm detail fail:", e);
			return new BsmResult(false, "查询详情失败", "", "");
		}
	}
}
